#!/bin/bash

source_functions()
{
	. conf/itennis.conf
	. scripts/log_print
	. scripts/exec_remote
	
}
show_help()
{
 	echo
	echo "Usage: remote_cmd parameter1"
	echo "以下变量说明:"
	echo "    parameter1:执行语句(shell命令)"
	echo
	return 0
}

main()
{
	source_functions
	local next=1
	local total=`grep -v "^#" conf/hosts.list | wc -l`
	
	timeout=$REMOTE_TIMEOUT
	port=$REMOTE_PORT
	
	echo "#管理地址	CPU虚拟核数	内存(G)	主机逻辑磁盘数量	磁盘情况" > datas/lld.conf
	grep -v "^#" conf/hosts.list | while read line
	do
		mip=`echo $line | awk '{print $1}'`
		sip=`echo $line | awk '{print $2}'`
		username=`echo $line | awk '{print $3}'`
		password=`echo $line | awk '{print $4}'`
		
		ping -c 3 -i 0.2 -W 3 $mip &> /dev/null
		if [ $? -eq 0 ];then
			log INFO "ping $mip success"
		else
			log ERROR "ping $mip failed"
			echo "$mip	N/A	N/A	N/A	N/A" | tee -a datas/lld.conf
			continue
		fi
		ping -c 3 -i 0.2 -W 3 $sip &> /dev/null
		if [ $? -eq 0 ];then
			log INFO "ping $sip success"
		else
			log ERROR "ping $sip failed"
			echo "$sip	N/A	N/A	N/A	N/A" | tee -a datas/lld.conf
			continue
		fi
		
		cup_core_cmd="cat /proc/cpuinfo |grep 'processor'|sort -u|wc -l"
		cpu_core=`exec_cmd_r $port $timeout $mip $username $password "$cup_core_cmd"`		
		#echo -e "CPUCORE:\t\c" &&  exec_cmd_r $port $timeout $mip $username $password "$cup_core_cmd" &> /dev/null
		
		mem_cmd="cat /proc/meminfo | grep MemTotal | tr -cd '[0-9]'"
		mem_num=`exec_cmd_r $port $timeout $mip $username $password "$mem_cmd"`
		mem=`echo "$mem_num" | awk '{print int($0/1024/1024)}'`
		#echo -e "MEMORYS:\t\c" && exec_cmd_r $port $timeout $mip $username $password "$mem_cmd" | awk '{print int($0/1024/1024)}' &> /dev/null
		
		disk_cmd="fdisk -l | grep 'Disk /dev/' | grep -iv 'Disk /dev/mapper'|wc -l"
		disk_num=`exec_cmd_r $port $timeout $mip $username $password "$disk_cmd"`
		
		disk=`echo "$disk_num" | grep -v 'WARNING'`
		#echo -e "DISKNUM:\t\c" && exec_cmd_r $port $timeout $mip $username $password "$disk_cmd"| grep -v 'WARNING' &> /dev/null
		
		disksize_cmd="fdisk -l | grep 'Disk /dev/' | grep -iv 'Disk /dev/mapper'"
		exec_cmd_r $port $timeout $mip $username $password "$disksize_cmd"| grep -v 'WARNING'|awk '{print $2 $3 $4}' | awk '/,/{printf $0}' > /tmp/itennis_disk
		#echo -e "DISKINFO:\t\c" && cat /tmp/itennis_disk &> /dev/null && echo ""
		disk_size=`cat /tmp/itennis_disk`
		
		log INFO ">>$mip already processed,current:$next total:$total"
		next=`echo $next|awk '{print int($0+1)}'`
		echo "$mip      $cpu_core       $mem    $disk   $disk_size" | tr -d '\r'
		echo "$mip	$cpu_core	$mem	$disk	$disk_size" | tr -d '\r' >> datas/lld.conf
	done
	#echo "#管理地址 CPU虚拟核数     内存(G) 主机逻辑磁盘数量        磁盘情况"  > datas/lld.txt
	#cat -v datas/lld.conf | grep -v "^#" | tr -d '^M'  >> datas/lld.txt
}
umask 027
main "$@"
exit $?
